import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [{
    path: '/',
    redirect: '/home'
  },

  {
    path: '/home',
    name: 'home',
    redirect: `/home/homePage/-1`,
    component: () => import( /* webpackChunkName: "about" */ '../pages/home/homeView.vue'),
    children: [{
        path: 'homePage/:userId',
        name: 'homePage',
        component: () => import( /* webpackChunkName: "about" */ '../pages/home/views/homePage.vue')
      },
      {
        path: 'userPage/:userId',
        name: 'userPage',
        component: () => import( /* webpackChunkName: "about" */ '../pages/home/views/userPage.vue')
      },
      {
        path: 'settingPage/:userId',
        name: 'settingPage',
        component: () => import( /* webpackChunkName: "about" */ '../pages/home/views/settingPage.vue')
      },
      {
        path: 'concreteScene/:scene/:userId/:sceneID/:city',
        name: 'concreteScene',
        component: () => import( /* webpackChunkName: "about" */ '../pages/home/views/concreteScene.vue')
      },
      {
        path: 'foodPage/:scene/:userId/:city',
        name: 'foodPage',
        component: () => import( /* webpackChunkName: "about" */ '../pages/home/views/foodPage.vue')
      },
      {
        path: 'shopPage/:scene/:userId/:city',
        name: 'shopPage',
        component: () => import( /* webpackChunkName: "about" */ '../pages/home/views/shopPage.vue')
      },
      {
        path: 'selectScene/:userId',
        name: 'selectScene',
        component: () => import( /* webpackChunkName: "about" */ '../pages/home/views/selectScene.vue')
      },
      {
        path: 'selectScheme/:userId',
        name: 'selectScheme',
        component: () => import( /* webpackChunkName: "about" */ '../pages/home/views/selectScheme.vue')
      },
      {
        path: 'navigationPage/:userId/:scene/:coordinate',
        name: 'navigationPage',
        component: () => import( /* webpackChunkName: "about" */ '../pages/home/views/navigationPage.vue')
      },
      {
        path: 'searchPage/:userId/:content',
        name: 'searchPage',
        component: () => import( /* webpackChunkName: "about" */ '../pages/home/views/searchPage.vue')
      },
      {
        path: 'schemePage',
        name: 'schemePage',
        component: () => import( /* webpackChunkName: "about" */ '../pages/home/views/schemePage.vue')
      },
    ]
  },
  {
    path: '/start',
    name: 'start',
    redirect: '/start/LoginView',
    component: () => import( /* webpackChunkName: "about" */ '../pages/start/startView.vue'),
    children: [{
      path: 'LoginView',
      name: 'LoginView',
      component: () => import( /* webpackChunkName: "about" */ '../pages/start/views/LoginView.vue')
    }, ]
  },
]

const router = new VueRouter({
  routes
})

// JWT 用户权限校验，判断 TOKEN 是否在 localStorage 当中
router.beforeEach(({name}, from, next) => {
  // 获取 JWT Token
  if (localStorage.getItem('JWT_TOKEN')) {
    // 如果用户在login页面
      next();
  } else {
    if (name === 'homePage') {
      next();
    }else if (name === 'LoginView') {
      next();
    } else {
      next({name: 'LoginView'});
    }
  }
});

export default router